﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ChairtyBank.Charity_BankDataSetTableAdapters;
using ChairtyBank.DAL;

namespace ChairtyBank
{
    public partial class ReportsForm : Form
    {
        public ReportsForm()
        {
            InitializeComponent();
        }

        private void ReportsForm_Load(object sender, EventArgs e)
        {
            comboReports.Text = "------إختار-------";
            panelCustomerCode.Visible = false;
            panelFromToCode.Visible = false;
            panelFromToDate.Visible = false;
            panelAkhesaay.Visible = false;
            LoadAkhesa2yComboBox();

            //Filling Adapters from dataSetsts
            
            
            
        }

        private void pkrStartDate_ValueChanged(object sender, EventArgs e)
        {
            DateTimePicker picker = (DateTimePicker)sender;
            picker.Value = new DateTime (picker.Value.Year, picker.Value.Month, picker.Value.Day, 0, 0, 0);
            
        }

        private void pkrEndDate_ValueChanged(object sender, EventArgs e)
        {
            DateTimePicker picker = (DateTimePicker)sender;
            picker.Value = new DateTime(picker.Value.Year, picker.Value.Month, picker.Value.Day, 23, 59, 59);
        }

        private void btnDisplayFromCombo_Click(object sender, EventArgs e)
        {
            if (comboReports.Text == "------إختار-------")
            {
                MessageBox.Show("عفواً وإختار نوع التقرير اولا"); 
                return;
            }
            if (comboReports.Text == "وارد خلال فتره")
            {
                this.RprTotaltPayed1.SetDataSource(charity_BankDataSet1);
                totalPayedQueryTableAdapter1.Fill(charity_BankDataSet1.TotalPayedQuery, pkrStartDate.Value, pkrEndDate.Value);
                this.RprTotaltPayed1.SetParameterValue("start Date", pkrStartDate.Value.Date);
                this.RprTotaltPayed1.SetParameterValue("End Date", pkrEndDate.Value.Date);
                crystalReportViewer1.ReportSource = this.RprTotaltPayed1;
                return;
            }

            if (comboReports.Text == "مدفوع خلال فتره")
            {
                this.RprtPayed1.SetDataSource(charity_BankDataSet1);
                payedQueryTableAdapter1.Fill(charity_BankDataSet1.PayedQuery, pkrStartDate.Value, pkrEndDate.Value);
                this.RprtPayed1.SetParameterValue("Start Date", pkrStartDate.Value);
                this.RprtPayed1.SetParameterValue("End Date", pkrEndDate.Value);
                crystalReportViewer1.ReportSource = this.RprtPayed1;
                return;
            }

            if (comboReports.Text == "متأخرات خلال فتره")
            {
                this.RprtNotPayed1.SetDataSource(charity_BankDataSet1);
                notPayedQueryTableAdapter1.Fill(charity_BankDataSet1.NotPayedQuery, pkrStartDate.Value, pkrEndDate.Value);

                this.RprtNotPayed1.SetParameterValue("start Date", pkrStartDate.Value);
                this.RprtNotPayed1.SetParameterValue("End Date", pkrEndDate.Value);
                crystalReportViewer1.ReportSource = this.RprtNotPayed1;
                return;
            }

            if (comboReports.Text == "المستفدين")
            {
                this.rprtCustomerDetails1.SetDataSource(charity_BankDataSet1);
                finishedTableAdapter1.Fill(charity_BankDataSet1.Finished, pkrStartDate.Value, pkrEndDate.Value);

                this.rprtCustomerDetails1.SetParameterValue("1", pkrStartDate.Value.Date);
                this.rprtCustomerDetails1.SetParameterValue("2", pkrEndDate.Value.Date);

                crystalReportViewer1.ReportSource = this.rprtCustomerDetails1;
                return;
            }
            if (comboReports.Text == "القروض المنتهية خلال فتره")
            {
                this.rprFinishedByEndDate1.SetDataSource(charity_BankDataSet1);
                finishedTableAdapter1.FinishedByEndDate(charity_BankDataSet1.Finished, pkrStartDate.Value, pkrEndDate.Value);
                this.rprFinishedByEndDate1.SetParameterValue("1", pkrStartDate.Value.Date);
                this.rprFinishedByEndDate1.SetParameterValue("2", pkrEndDate.Value.Date);
                crystalReportViewer1.ReportSource = this.rprFinishedByEndDate1;
                return;
            }
            if (comboReports.Text == "القروض السارية خلال فتره")
            {
                this.rprNotFinishedByPayDate1.SetDataSource(charity_BankDataSet1);
                finishedTableAdapter1.NotFinishedByPayDate(charity_BankDataSet1.Finished, pkrStartDate.Value, pkrEndDate.Value);

                this.rprNotFinishedByPayDate1.SetParameterValue("1", pkrStartDate.Value.Date);
                this.rprNotFinishedByPayDate1.SetParameterValue("2", pkrEndDate.Value.Date);
                crystalReportViewer1.ReportSource = this.rprNotFinishedByPayDate1;
                return;
            }
            if (comboReports.Text == "القروض المنتهية بكود العميل")
            {
                String from = txt_C_ID_from.Text == "" ? "1" : txt_C_ID_from.Text;
                String to = txt_c_id_to.Text == "" ? "10000" : txt_c_id_to.Text;
                this.rprFinished1.SetDataSource(charity_BankDataSet1);
                finishedTableAdapter1.Finished(charity_BankDataSet1.Finished,int.Parse(from),int.Parse(to));
                this.rprFinished1.SetParameterValue("1", from);
                this.rprFinished1.SetParameterValue("2", to);
                crystalReportViewer1.ReportSource = this.rprFinished1;
                return;
            }

            if (comboReports.Text.Trim() == "القروض السارية بكود العميل".Trim())
            {
                String from = txt_C_ID_from.Text == "" ? "1" : txt_C_ID_from.Text;
                String to = txt_c_id_to.Text == "" ? "10000" : txt_c_id_to.Text;
                this.rprNotFinished1.SetDataSource(charity_BankDataSet1);
                finishedTableAdapter1.notFinished(charity_BankDataSet1.Finished, int.Parse(from), int.Parse(to));
                this.rprNotFinished1.SetParameterValue("1", from);
                this.rprNotFinished1.SetParameterValue("2", to);
                crystalReportViewer1.ReportSource = this.rprNotFinished1;
                return;
            }
            if (comboReports.Text.Trim() == "بيانات عميل".Trim())
            {
                this.rprtPersons1.SetDataSource(charity_BankDataSet1);
               
                if (txtCustomerCode.Text != "")
                {
                    allDataQueryTableAdapter1.FillBy(charity_BankDataSet1.AllDataQuery, int.Parse(txtCustomerCode.Text));
                    crystalReportViewer1.ReportSource = this.rprtPersons1;
                }
                else
                {
                    MessageBox.Show(" يجب إدخال رقم العميل في خانةً من");
                }
                return;
            }

            if (comboReports.Text == "المقترضين أكثر من مره")
            {
                this.rprtMultiLoans1.SetDataSource(charity_BankDataSet1);
                finishedTableAdapter1.MultiLoans(charity_BankDataSet1.Finished);
                crystalReportViewer1.ReportSource = this.rprtMultiLoans1;
                return;
            }
            if (comboReports.Text == "اللجنه القانونية")
            {
                RprtLaw rpt = new RprtLaw();
                Charity_BankDataSet dataSet = new Charity_BankDataSet();
                DataTable1TableAdapter adapter = new DataTable1TableAdapter();
                adapter.Fill(dataSet.DataTable1, pkrStartDate.Value.Date, pkrEndDate.Value.Date);
                rpt.SetDataSource(dataSet);
                if (pkrStartDate.Value.Date != pkrEndDate.Value.Date)
                {
                    rpt.SetParameterValue("start date", pkrStartDate.Value.Date);
                    rpt.SetParameterValue("end date", pkrEndDate.Value.Date);
                    crystalReportViewer1.ReportSource = rpt;
                }
                else
                {
                    MessageBox.Show("يجب إدخال التاريخ المطلوب ");
                }
                return;
            }
            if (comboReports.Text == "القروض السارية بالأخصائي")
            {
                RprtNotFinishedByAkhesaay report = new RprtNotFinishedByAkhesaay();
                report.SetDataSource(charity_BankDataSet1);
                NotFinishedByAkhesaayTableAdapter byAkhesaye = new NotFinishedByAkhesaayTableAdapter();
                byAkhesaye.Fill(charity_BankDataSet1.NotFinishedByAkhesaay, comboAkhresaay.Text);
                report.SetParameterValue("Akhesaye", comboAkhresaay.Text);
                crystalReportViewer1.ReportSource = report;
                return;
            }
           
        }

        private void comboReports_SelectionChangeCommitted(object sender, EventArgs e)
        {
            panelFromToCode.Visible = false;
            panelFromToDate.Visible = false;
            panelCustomerCode.Visible = false;
            panelAkhesaay.Visible = false;
            if (comboReports.Text == "وارد خلال فتره")
            {
                panelFromToDate.Visible = true;
                return;
            }

            if (comboReports.Text == "مدفوع خلال فتره")
            {
                panelFromToDate.Visible = true;
                return;
            }

            if (comboReports.Text == "متأخرات خلال فتره")
            {
                panelFromToDate.Visible = true;
                return;
            }

            if (comboReports.Text == "المستفدين")
            {
                panelFromToDate.Visible = true;
                return;
            }
            if (comboReports.Text == "القروض المنتهية خلال فتره")
            {
                panelFromToDate.Visible = true;
                return;
            }
            if (comboReports.Text == "القروض السارية خلال فتره")
            {
                panelFromToDate.Visible = true;
                return;
            }
             if (comboReports.Text == "القروض السارية بالأخصائي")
            {
                panelAkhesaay.Visible = true;
                return;
            }
            
            if (comboReports.Text == "القروض المنتهية بكود العميل")
            {
                panelFromToCode.Visible = true;
                return;
            }

            if (comboReports.Text.Trim() == "القروض السارية بكود العميل".Trim())
            {
                panelFromToCode.Visible = true;
                return;
            }
            if (comboReports.Text.Trim() == "بيانات عميل".Trim())
            {
                panelCustomerCode.Visible = true;
                return;
            }

            if (comboReports.Text == "المقترضين أكثر من مره")
            {
                return;
            }
            if (comboReports.Text == "اللجنه القانونية")
            {
                panelFromToDate.Visible = true;
                return;
            }
        }

        private void LoadAkhesa2yComboBox()
        {
            comboAkhresaay.DataSource = Akhes.GetAll();
            comboAkhresaay.DisplayMember = "name";
        }
    }
}
